<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Canvas画板</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <canvas class="canvas"></canvas>
    <script>
        let canvas = document.querySelector(".canvas");
        canvas.width = document.documentElement.clientWidth;
        canvas.height = document.documentElement.clientHeight;
        let ctx = canvas.getContext("2d")
        ctx.fillStyle = "black"
        ctx.strokeStyle = "black"
        ctx.lineCap = "round";
        ctx.lineWidth = 10



        //画线

        function drawLine(x1, y1, x2, y2) {
            ctx.beginPath()
            ctx.moveTo(x1, y1)
            ctx.lineTo(x2, y2)


            ctx.stroke()

        }


        var isTouchDevice = 'ontouchstart' in document.documentElement;
        console.log(isTouchDevice)
        let painting = false;
        let last;
        /*在微信浏览器上会有bug*/
        if (isTouchDevice) {
            canvas.ontouchstart = (e) => {
                let x = e.touches[0].clientX
                let y = e.touches[0].clientY

                last = [x, y]

            }
            canvas.ontouchmove = (e) => {
                let x = e.touches[0].clientX
                let y = e.touches[0].clientY
                drawLine(last[0], last[1], x, y)
                last = [x, y]

            }

        } else {

            canvas.onmousedown = (e) => {
                last = [e.clientX, e.clientY]
                painting = true;
            }


            canvas.onmousemove = (e) => {
                if (painting === true) {
                    // ctx.beginPath();
                    // ctx.arc(e.clientX, e.clientY, 10, 0, 2 * Math.PI)
                    // ctx.stroke()
                    // ctx.fill()
                    drawLine(last[0], last[1], e.clientX, e.clientY)
                    last = [e.clientX, e.clientY]
                }

            }
            canvas.onmouseup = (e) => {
                painting = false;
            }
        }
    </script>
</body>

</html>